home *** CD-ROM | disk | FTP | other *** search
/ Mac Expert 1995 Winter / Mac Expert - Winter 95.iso / Les fichiers / Communications / Internet / TurboTCP 2.1 ƒ / TCL MiniTelnet source / CTelnetSettingsDLOG.cp < prev    next >
Encoding:
Text File  |  1995-01-04  |  8.7 KB  |  437 lines  |  [TEXT/MMCC]

  1. /*
  2. ** CTelnetSettingsDLOG.cp
  3. **
  4. **    MiniTelnet application
  5. **    Telnet settings dialog director
  6. **
  7. **    Copyright © 1993-94, FrostByte Design / Eric Scouten
  8. **
  9. */
  10.  
  11.  
  12. #include "CTelnetSettingsDLOG.h"
  13.  
  14. #include "CApplication.h"
  15. #include "CBartender.h"
  16. #include "CDialog.h"
  17. #include "CButton.h"
  18. #include "CCheckBox.h"
  19. #include "CDataFile.h"
  20. #include "CEditText.h"
  21. #include "CPaneBorder.h"
  22. #include "CRadioGroupPane.h"
  23. #include "Commands.h"
  24. #include "TBUtilities.h"
  25. #include "TCLUtilities.h"
  26.  
  27. #include "CMiniTelnetApp.h"
  28. #include "CTelnetTerminal.h"
  29.  
  30.  
  31. // item numbers in dialog box
  32.  
  33. #define itemOK            1
  34. #define itemCancel        2
  35. #define itemSave        3
  36. #define itemHostName    4
  37. #define itemBSPane        6
  38. #define itemBS_BS        7
  39. #define itemBS_DEL        8
  40. #define itemGoAway        9
  41. #define itemTitle        10
  42. #define itemShowDebug    14
  43.  
  44. extern CApplication*    gApplication;
  45. extern CBartender*        gBartender;
  46. extern OSType            gSignature;
  47.  
  48.  
  49. //    —— constructor ——
  50.  
  51. /*______________________________________________________________________
  52. **
  53. ** constructor
  54. **
  55. **    Initialize the settings dialog.
  56. **
  57. **        aSupervisor (CDirectorOwner*):    the supervisor for the dialog
  58. **
  59. */
  60.  
  61. CTelnetSettingsDLOG::CTelnetSettingsDLOG (CDirectorOwner* aSupervisor)
  62.     : CDLOGDirector(DLOGSettings, aSupervisor)
  63.  
  64. {
  65.     CButton*        theButton;
  66.     CPane*        thePane;
  67.     CPaneBorder*    theBorder;
  68.     CView*        theView;
  69.  
  70.  
  71.     // find important buttons & configure them
  72.  
  73.     theView = itsWindow->FindViewByID(itemOK);
  74.     theButton = TCL_DYNAMIC_CAST(CButton, theView);
  75.     if (theButton) {
  76.         theButton->SetClickCmd(cmdOK);
  77.         ((CDialog*) itsWindow)->SetDefaultButton(theButton);
  78.     }
  79.  
  80.     theView = itsWindow->FindViewByID(itemCancel);
  81.     theButton = TCL_DYNAMIC_CAST(CButton, theView);
  82.     if (theButton)
  83.         theButton->SetClickCmd(cmdCancel);
  84.  
  85.     theView = itsWindow->FindViewByID(itemSave);
  86.     theButton = TCL_DYNAMIC_CAST(CButton, theView);
  87.     if (theButton)
  88.         theButton->SetClickCmd(cmdSaveSettings);
  89.  
  90.  
  91.     // add a border for the dialog title
  92.  
  93.     theView = itsWindow->FindViewByID(itemTitle);
  94.     thePane = TCL_DYNAMIC_CAST(CPane, theView);
  95.     if (thePane) {
  96.         theBorder = new CPaneBorder(kBorderBottom);
  97.         thePane->SetBorder(theBorder);
  98.     }
  99.  
  100.  
  101.     // get rid of that ugly border around the radio buttons
  102.  
  103.     theView = itsWindow->FindViewByID(itemBSPane);
  104.     thePane = TCL_DYNAMIC_CAST(CPane, theView);
  105.     if (thePane) {
  106.         theBorder = thePane->GetBorder();
  107.         if (theBorder)
  108.             theBorder->SetBorderFlags(kBorderNone);
  109.     }
  110.     
  111. }
  112.  
  113.  
  114. //    —— settings record functions ——
  115.  
  116. /*______________________________________________________________________
  117. **
  118. ** DefaultSettings
  119. **
  120. **    Creates a new blank settings record and copies this to the dialog.
  121. **
  122. */
  123.  
  124. void CTelnetSettingsDLOG::DefaultSettings()
  125.  
  126. {
  127.     r.signature = kTSRsignature;
  128.     r.settingsVersion = kTSRversion;
  129.     r.settingsMinVersion = 0;
  130.     r.hostName[0] = '\0';
  131.     BlockMove("UNKNOWN", &r.termEmulation, 8);
  132. //    r.backspaceChar = charBS;
  133.     r.backspaceChar = charDEL;
  134.     r.closeOnSessionEnd = FALSE;
  135.     r.showDebug = FALSE;
  136.     
  137.     PutSettings();
  138. }
  139.  
  140.  
  141. /*______________________________________________________________________
  142. **
  143. ** GrabSettings
  144. **
  145. **    Get the settings from the dialog box fields and put them into the
  146. **    settings record.
  147. **
  148. */
  149.  
  150. void CTelnetSettingsDLOG::GrabSettings()
  151.  
  152. {
  153.     CCheckBox*        theCheckBox;
  154.     CEditText*        theText;
  155.     CRadioGroupPane*    theRadios;
  156.     CView*            theView;
  157.     short              textSize;
  158.     short            i;
  159.     char*            p;
  160.  
  161.  
  162.     // blank out the settings record
  163.     
  164.     p = (char*) &r;
  165.     for (i=0; i<sizeof(TelnetSettingsRec); i++)
  166.         *p++ = '\0';
  167.  
  168.  
  169.     // recreate signature, version info
  170.  
  171.     r.signature = kTSRsignature;
  172.     r.settingsVersion = kTSRversion;
  173.     r.settingsMinVersion = 0;
  174.  
  175.  
  176.     // get host name
  177.  
  178.     theView = itsWindow->FindViewByID(itemHostName);
  179.     theText = TCL_DYNAMIC_CAST(CEditText, theView);
  180.     if (theText) {
  181.         textSize = theText->GetLength();
  182.         textSize = textSize > 255 ? 255 : textSize;
  183.         BlockMove(*(theText->GetTextHandle()), &r.hostName, textSize);
  184.         r.hostName[textSize] = '\0';
  185.     }
  186.  
  187.  
  188.     // get terminal emulation
  189.  
  190.     BlockMove("UNKNOWN", &r.termEmulation, 8);
  191.  
  192.  
  193.     // get backspace/del configuration
  194.  
  195.     theView = itsWindow->FindViewByID(itemBSPane);
  196.     theRadios = TCL_DYNAMIC_CAST(CRadioGroupPane, theView);
  197.     if (theRadios)
  198.         r.backspaceChar = (theRadios->GetStationID() == itemBS_DEL ? charDEL : charBS);
  199.  
  200.  
  201.     // get window go away config
  202.  
  203.     theView = itsWindow->FindViewByID(itemGoAway);
  204.     theCheckBox = TCL_DYNAMIC_CAST(CCheckBox, theView);
  205.     if (theCheckBox)
  206.         r.closeOnSessionEnd = theCheckBox->IsChecked();
  207.  
  208.  
  209.     // get show debug codes config
  210.  
  211.     theView = itsWindow->FindViewByID(itemShowDebug);
  212.     theCheckBox = TCL_DYNAMIC_CAST(CCheckBox, theView);
  213.     if (theCheckBox)
  214.         r.showDebug = theCheckBox->IsChecked();
  215.  
  216. }
  217.  
  218.  
  219. /*______________________________________________________________________
  220. **
  221. ** PutSettings
  222. **
  223. **    Copy the settings from the settings record to the dialog box fields.
  224. **
  225. */
  226.  
  227. void CTelnetSettingsDLOG::PutSettings()
  228.  
  229. {
  230.     CCheckBox*        theCheckBox;
  231.     CEditText*        theText;
  232.     CPane*            thePane;
  233.     CRadioGroupPane*    theRadios;
  234.     CView*            theView;
  235.  
  236.  
  237.     // get host name
  238.  
  239.     theView = itsWindow->FindViewByID(itemHostName);
  240.     theText = TCL_DYNAMIC_CAST(CEditText, theView);
  241.     if (theText)
  242.         theText->SetTextPtr((char*) &r.hostName, cstrlen(r.hostName));
  243.  
  244.  
  245.     // get backspace/del configuration
  246.  
  247.     theView = itsWindow->FindViewByID(itemBSPane);
  248.     theRadios = TCL_DYNAMIC_CAST(CRadioGroupPane, theView);
  249.     if (theRadios)
  250.         theRadios->SetStationID((r.backspaceChar == charDEL) ? itemBS_DEL : itemBS_BS);
  251.  
  252.  
  253.     // get window go away config
  254.  
  255.     theView = itsWindow->FindViewByID(itemGoAway);
  256.     theCheckBox = TCL_DYNAMIC_CAST(CCheckBox, theView);
  257.     if (theCheckBox)
  258.         theCheckBox->SetValue(r.closeOnSessionEnd);
  259.  
  260.  
  261.     // get show debug codes config
  262.  
  263.     theView = itsWindow->FindViewByID(itemShowDebug);
  264.     theCheckBox = TCL_DYNAMIC_CAST(CCheckBox, theView);
  265.     if (theCheckBox)
  266.         theCheckBox->SetValue(r.showDebug);
  267.  
  268.  
  269.     // ignore other parms
  270.  
  271.     // force redraw
  272.  
  273.     thePane = TCL_DYNAMIC_CAST(CPane, itsWindow);
  274.     if (thePane)
  275.         thePane->Refresh();
  276.  
  277. }
  278.  
  279.  
  280. //    —— dialog handling functions ——
  281.  
  282. /*______________________________________________________________________
  283. **
  284. ** DoCommand
  285. **
  286. **    Handle all commands that the dialog can understand.
  287. **
  288. **        theCommand (long):    the command number which was issued
  289. **
  290. */
  291.  
  292. void CTelnetSettingsDLOG::DoCommand(long theCommand)
  293.  
  294. {
  295.     CButton*            cancelBtn;
  296.     TelnetSettingsRec    rTemp;
  297.  
  298.  
  299.     // what command did we get?
  300.  
  301.     switch (theCommand) {
  302.  
  303.  
  304.         // OK, Cancel buttons: respond
  305.  
  306.         case cmdOK:
  307.             if (EndDialog(cmdOK, TRUE)) {
  308.                 GrabSettings();
  309.                 BlockMove(&r, &rTemp, sizeof(TelnetSettingsRec));
  310.                 if (TCL_DYNAMIC_CAST(CMiniTelnetApp, gApplication))
  311.                     ((CMiniTelnetApp*) gApplication)->NewSession(&rTemp);
  312.             }
  313.             break;
  314.             
  315.         case cmdCancel:
  316.             EndDialog(cmdCancel, FALSE);
  317.             break;
  318.  
  319.  
  320.         // Save Settings: do it
  321.  
  322.         case cmdSave:
  323.         case cmdSaveAs:
  324.         case cmdSaveSettings:
  325.             GrabSettings();
  326.             DoSaveFile();
  327.             break;
  328.  
  329.  
  330.         // File->Close: cancel out of here
  331.  
  332.         case cmdClose:        
  333.             cancelBtn = ((CDialog*) itsWindow)->FindButton(cmdCancel);
  334.             if (cancelBtn)
  335.                 cancelBtn->SimulateClick();
  336.             EndDialog(cmdCancel, FALSE);
  337.             break;
  338.  
  339.  
  340.         // not ours, send along the chain
  341.  
  342.         default:
  343.             CDialogDirector::DoCommand(theCommand);
  344.     }
  345.  
  346.  
  347.     // if closing window, use a disposer chore
  348.  
  349.     if ((dismissCmd != cmdNull) && (theCommand != cmdClose))
  350.         delete this;
  351.  
  352. }
  353.  
  354.  
  355. /*______________________________________________________________________
  356. **
  357. ** UpdateMenus
  358. **
  359. **    Enable Telnet-specific commands.
  360. **
  361. */
  362.  
  363. void CTelnetSettingsDLOG::UpdateMenus()
  364.  
  365. {
  366.     CDLOGDirector::UpdateMenus();
  367.     gBartender->EnableCmd(cmdSave);
  368.     gBartender->EnableCmd(cmdSaveAs);
  369. }
  370.  
  371.  
  372. //    —— file interactions ——
  373.  
  374. /*______________________________________________________________________
  375. **
  376. ** DoSaveFile
  377. **
  378. **    Save a settings record to a file.
  379. **
  380. */
  381.  
  382. void CTelnetSettingsDLOG::DoSaveFile()
  383.  
  384. {
  385.     Point            corner;                // top left corner of dialog box
  386.     Str255        origName;                // default name for file
  387.     short        nameLength;            // length of default name
  388.     StringHandle    prompt;                // prompt string
  389.     Boolean        wasLocked;
  390.     
  391.     CDataFile*    itsFile;                // don’t keep the file around
  392.     SFReply        macSFReply;            // reply from Std File
  393.  
  394.  
  395.     // ask user for settings file name
  396.  
  397.     FindDlogPosition('DLOG', putDlgID, &corner);
  398.     BlockMove(&r.hostName, &origName[1], 31);
  399.     nameLength = cstrlen(r.hostName);
  400.     origName[0] = (nameLength > 31 ? 31 : nameLength);
  401.     
  402.     prompt = GetString(STR_SettingsPrompt);
  403.     FailNILRes(prompt);
  404.     
  405.     MoveHHi((Handle) prompt);
  406.     HLock((Handle) prompt);
  407.     SFPPutFile(corner, *prompt, origName, NULL, &macSFReply, putDlgID, NULL);
  408.     ReleaseResource((Handle) prompt);
  409.  
  410.  
  411.     // create the file
  412.  
  413.     TRY {
  414.         itsFile = new CDataFile;
  415.         itsFile->SFSpecify(&macSFReply);
  416.  
  417.         if (itsFile->ExistsOnDisk()) {
  418.             itsFile->Open(fsRdWrPerm);
  419.             itsFile->SetLength(0);
  420.                 // TEMPORARY: need to reset file type
  421.         }
  422.         else {
  423.             itsFile->CreateNew(gSignature, kSettingsFileType);
  424.             itsFile->Open(fsRdWrPerm);
  425.         }
  426.         itsFile->WriteSome((Ptr) &r, sizeof(TelnetSettingsRec));
  427.         itsFile->Close();
  428.         delete itsFile;
  429.     }
  430.     
  431.     CATCH {
  432.         ForgetObject(itsFile);
  433.     }
  434.     ENDTRY;
  435.  
  436. }
  437.